package com.lyy.mapper;


import com.lyy.entity.OrderDetail;

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Liangyaoyun
 */
@Repository
public interface OrderDetailMapper {
    String list = "select od.order_detail_id, od.fk_order_id, od.fk_video_id, od.fk_user_id, " +
            "od.create_time, od.modify_time " +
            "from lesson.order_detail od ";

    /**
     * 添加一条视频订单中间表记录
     *
     * @param orderDetail 添加字段
     * @return 影响条目数
     */
    @Insert("insert into lesson.order_detail (" +
            "fk_order_id, fk_video_id, fk_user_id, create_time, modify_time) " +
            "values (" +
            "#{fkOrderId}, #{fkVideoId}, #{fkUserId}, #{createTime}, #{modifyTime})")
    @Options(useGeneratedKeys = true, keyProperty = "orderDetailId")
    int insert(OrderDetail orderDetail);

    /**
     * 按条件查询视频订单中间表记录，分步查询对应的用户记录，订单记录和视频记录
     *
     * @param condition 查询条件
     * @return 查询失败时返回空List对象
     */
    @Results({
            @Result(id = true, column = "order_detail_id", property = "orderDetailId"),
            @Result(column = "fk_order_id", property = "fkOrderId"),
            @Result(column = "fk_video_id", property = "fkVideoId"),
            @Result(column = "fk_user_id", property = "fkUserId"),
            @Result(column = "fk_order_id", property = "order",
                    one = @One(select = "com.lyy.mapper.OrderMapper.selectByOrderId")),
            @Result(column = "fk_video_id", property = "video",
                    one = @One(select = "com.lyy.mapper.VideoMapper.selectByVideoId")),
            @Result(column = "fk_user_id", property = "user",
                    one = @One(select = "com.lyy.mapper.UserMapper.selectByUserId"))
    })
    @Select("<script>" + list +
            "<where> " +
            "<if test='orderDetailId != null'>and od.order_detail_id = #{orderDetailId}</if> " +
            "<if test='fkOrderId != null'>and od.fk_order_id = #{fkOrderId}</if> " +
            "<if test='fkVideoId != null'>and od.fk_video_id = #{fkVideoId}</if> " +
            "<if test='fkUserId != null'>and od.fk_user_id = #{fkUserId}</if> " +
            "<if test='createTime != null'>and od.create_time = #{createTime}</if> " +
            "<if test='modifyTime != null'>and od.modify_time = #{modifyTime}</if> " +
            "</where>" +
            "</script>")
    List<OrderDetail> selectDetail(OrderDetail condition);

    /**
     * 按条件删除视频订单中间表记录
     *
     * @param condition 删除条件
     * @return 影响条目数
     */
    @Delete("<script>" +
            "delete from lesson.order_detail " +
            "<where> " +
            "<if test='orderDetailId != null'>and order_detail_id = #{orderDetailId}</if> " +
            "<if test='fkOrderId != null'>and fk_order_id = #{fkOrderId}</if> " +
            "<if test='fkVideoId != null'>and fk_video_id = #{fkVideoId}</if> " +
            "<if test='fkUserId != null'>and fk_user_id = #{fkUserId}</if> " +
            "<if test='createTime != null'>and create_time = #{createTime}</if> " +
            "<if test='modifyTime != null'>and modify_time = #{modifyTime}</if> " +
            "</where>" +
            "</script>")
    int delete(OrderDetail condition);






    /*
     * 插入一条orderDetail记录
     *
     * @param orderDetail
     * @return
     *//*
    @Insert("insert into lesson.order_detail (fk_order_id, fk_video_id, fk_user_id,create_time,modify_time)" +
            " VALUES (#{fkOrderId}, #{fkVideoId}, #{fkUserId},  #{createTime}, #{modifyTime})")
    int insert(orderDetail orderDetail);

    *//**
     * 删除orderDetail记录
     *
     * @param orderDetail orderDetail实体
     * @return 影响条目数
     *//*
    @Delete("<script>" +
            "delete from lesson.order_detail " +
            "<where>" +
            "<if test='orderDetailId != null'> and order_detail_id = #{orderDetailId}</if>" +
            "<if test='fkOrderId != null'> and fk_order_id = #{fkOrderId}</if>" +
            "<if test='fkVideoId != null'> and fk_video_id = #{fkVideoId}</if>" +
            "<if test='fkUserId != null'> and fk_user_id = #{fkUserId}</if>" +
            "<if test='createTime != null'> and create_time = #{createTime}</if>" +
            "<if test='modifyTime != null'> and modify_time = #{modifyTime}</if>" +
            "</where>" +
            "</script>")
    int delete(orderDetail orderDetail);

    *//**
     * 查询orderDetail记录
     * 1.分步查询Video/Order/User表
     *
     * @param orderDetail orderDetail实体
     * @return 多条orderDetail记录
     *//*
    @Results({
            @Result(id = true, property = "orderDetailId", column = "order_detail_id"),
            @Result(property = "fkOrderId", column = "fk_order_id"),
            @Result(property = "fkVideoId", column = "fk_video_id"),
            @Result(property = "fkUserId", column = "fk_user_id"),
            @Result(property = "video", column = "fk_video_id",
                    many = @Many(selectDetail = "com.jyb.mapper.VideoMapper.selectDetailByVideoId")),
            @Result(property = "order", column = "fk_order_id",
                    many = @Many(selectDetail = "com.jyb.mapper.OrderMapper.selectDetailByOrderId")),
            @Result(property = "user", column = "fk_user_id",
                    many = @Many(selectDetail = "com.jyb.mapper.UserMapper.selectDetailByUserId")),
    })
    @selectDetail("<script>" +
            "selectDetail * " +
            "from lesson.order_detail" +
            "<where>" +
            "<if test='orderDetailId != null'> and order_detail_id = #{orderDetailId}</if>" +
            "<if test='fkOrderId != null'> and fk_order_id = #{fkOrderId}</if>" +
            "<if test='fkVideoId != null'> and fk_video_id = #{fkVideoId}</if>" +
            "<if test='fkUserId != null'> and fk_user_id = #{fkUserId}</if>" +
            "<if test='createTime != null'> and create_time = #{createTime}</if>" +
            "<if test='modifyTime != null'> and modify_time = #{modifyTime}</if>" +
            "</where>" +
            "</script>")
    List<orderDetail> selectDetail(orderDetail orderDetail);

    *//*根据订单外键查询订单*//*
    //property对应的是Java中的实体类属性，column对应数据库里面的属性
    @Results({
            //第一条@Result是为了加速用的，可以不配
            @Result(id = true, property = "orderDetailId", column = "order_detail_id"),
            //property找实体类里面的属性,column传一个数据到另一个Mapper里面并执行对应的方法
            //column是将第一个表的查询的数据，要对应该表的数据库名字
            @Result(property = "orderList", column = "fk_order_id",
                    many = @Many(selectDetail = "com.lyy.mapper.OrderMapper.selectDetailByOrderId"))
    })
    *//*根据videoId查询到chapter表里的数据*//*
    @selectDetail("selectDetail * from lesson.order_detail where order_detail_id=#{param1}")
    orderDetail selectDetailByorderDetailIdAndOrder(Integer orderDetailId);

    *//*根据视频外键查询视频*//*
    //property对应的是Java中的实体类属性，column对应数据库里面的属性
    @Results({
            //第一条@Result是为了加速用的，可以不配
            @Result(id = true, property = "orderDetailId", column = "order_detail_id"),
            //property找实体类里面的属性,column传一个数据到另一个Mapper里面并执行对应的方法
            //column是将第一个表的查询的数据，要对应该表的数据库名字
            @Result(property = "videoList", column = "fk_video_id",
                    many = @Many(selectDetail = "com.lyy.mapper.VideoMapper.selectDetailByVideoId"))
    })
    *//*根据videoId查询到chapter表里的数据*//*
    @selectDetail("selectDetail * from lesson.order_detail where order_detail_id=#{param1}")
    orderDetail selectDetailByorderDetailIdAndVideo(Integer orderDetailId);

    *//*根据user外键查询视频*//*
    //property对应的是Java中的实体类属性，column对应数据库里面的属性
    @Results({
            //第一条@Result是为了加速用的，可以不配
            @Result(id = true, property = "orderDetailId", column = "order_detail_id"),
            //property找实体类里面的属性,column传一个数据到另一个Mapper里面并执行对应的方法
            //column是将第一个表的查询的数据，要对应该表的数据库名字
            @Result(property = "userList", column = "fk_user_id",
                    many = @Many(selectDetail = "com.lyy.mapper.UserMapper.selectDetailByUserId"))
    })
    *//*根据fk_user_id查询到user表里的数据*//*
    @selectDetail("selectDetail * from lesson.order_detail where order_detail_id=#{param1}")
    orderDetail selectDetailByorderDetailIdAndUser(Integer orderDetailId);

    @Insert("insert into lesson.order_detail(fk_order_id,fk_video_id,fk_user_id,create_time,modify_time)value(#{fkOrderId},#{fkVideoId},#{fkUserId},#{createTime},#{modifyTime})")
    @Options
    int insertorderDetail(orderDetail orderDetail);

    *//*根据user主键删除一条orderDetail记录*//*
    @Delete("delete from lesson.order_detail where fk_user_id=#{fkUserId}")
    int deleteByUserId(Integer userId);
    *//*根据order主键删除一条orderDetail记录*//*
    @Delete("delete from lesson.order_detail where fk_order_id=#{fkOrderId}")
    int deleteByOrderId(Integer orderId);
    *//*根据video主键删除一条orderDetail记录*//*
    @Delete("delete from lesson.order_detail where fk_video_id=#{fkVideoId}")
    int deleteByVideoId(Integer VideoId);
    *//*根据orderDetail主键删除一条orderDetail记录*//*
    @Delete("delete from lesson.order_detail where order_detail_id=#{orderDetailId}")
    int deleteByorderDetailId(Integer orderDetailId);

    *//*因为有延迟加载，所以它需要打印才能显示结果*//*
    @Results({
            //第一条@Result是为了加速用的，可以不配
            @Result(id = true, property = "orderDetailId", column = "order_detail_id"),
            //property找实体类里面的属性,column传一个数据到另一个Mapper里面并执行对应的方法
            //column是将第一个表的查询的数据，要对应该表的数据库名字
            @Result(property = "userList", column = "fk_user_id",
                    many = @Many(selectDetail = "com.lyy.mapper.UserMapper.selectDetailByUserId")),
            @Result(property = "videoList", column = "fk_video_id",
                    many = @Many(selectDetail = "com.lyy.mapper.VideoMapper.selectDetailByVideoId")),
            @Result(property = "orderList", column = "fk_order_id",
                    many = @Many(selectDetail = "com.lyy.mapper.OrderMapper.selectDetailByOrderId"))
    })
    *//*根据videoId查询到chapter表里的数据*//*
    @selectDetail("selectDetail * from lesson.order_detail where order_detail_id=#{param1}")
    orderDetail selectDetailByorderDetailIdAllDetail(Integer orderDetailId);

    *//*因为有延迟加载，所以它需要打印才能显示结果*//*
    @Results({
            //第一条@Result是为了加速用的，可以不配
            @Result(id = true, property = "orderDetailId", column = "order_detail_id"),
            //property找实体类里面的属性,column传一个数据到另一个Mapper里面并执行对应的方法
            //column是将第一个表的查询的数据，要对应该表的数据库名字
            @Result(property = "userList", column = "fk_user_id",
                    many = @Many(selectDetail = "com.lyy.mapper.UserMapper.selectDetailByUserId")),
            @Result(property = "videoList", column = "fk_video_id",
                    many = @Many(selectDetail = "com.lyy.mapper.VideoMapper.selectDetailByVideoId")),
            @Result(property = "orderList", column = "fk_order_id",
                    many = @Many(selectDetail = "com.lyy.mapper.OrderMapper.selectDetailByOrderId"))
    })
    @selectDetail("selectDetail * from lesson.order_detail where fk_order_id=#{param1}")
    List<orderDetail> selectDetailByOrderIdAllDetail(Integer fkOrderId);

    *//*因为有延迟加载，所以它需要打印才能显示结果*//*
    @Results({
            //第一条@Result是为了加速用的，可以不配
            @Result(id = true, property = "orderDetailId", column = "order_detail_id"),
            //property找实体类里面的属性,column传一个数据到另一个Mapper里面并执行对应的方法
            //column是将第一个表的查询的数据，要对应该表的数据库名字
            @Result(property = "user", column = "fk_user_id",
                    many = @Many(selectDetail = "com.lyy.mapper.UserMapper.selectDetailByUserId")),
            @Result(property = "video", column = "fk_video_id",
                    many = @Many(selectDetail = "com.lyy.mapper.VideoMapper.selectDetailByVideoId")),
            @Result(property = "order", column = "fk_order_id",
                    many = @Many(selectDetail = "com.lyy.mapper.OrderMapper.selectDetailByOrderId"))
    })
    @selectDetail("selectDetail * from lesson.order_detail where fk_user_id=#{param1}")
    List<orderDetail> selectDetailByUserIdAllDetail(Integer fkUserId);


    @selectDetail("<script>" +
            "selectDetail * from lesson.order_detail" +
            "<where>" +
            "fk_user_id=#{param1} and " +
            "<foreach collection='param2' " +
            "item='videoId' " +
            "open='fk_video_id in (' " +
            "close=')' " +
            "separator=','>" +
            "${videoId}" +
            "</foreach>" +
            "</where>" +
            "</script>")
    List<orderDetail> selectDetailByUserIdAndVideoIds(Integer userId,Integer[] videoIds);*/
}
